'use strict';

const Controller = require('egg').Controller;

class CarouselController extends Controller {
  /**
     *
     * @api {POST} http://localhost:7001/api/carousel/create 创建轮播图
     * @apiName 创建轮播图
     * @apiGroup 轮播图模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Array} carouselArr 图片数组
     * @apiParam  {Number} carouselGroupId 轮播图分组id
     *
     * @apiParamExample  {type} Request-Example:
        {
            "carouselArr":["https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg","https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.2.jpg"],
            "carouselGroupId":1
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "创建成功",
            "data": []
        }
     *
     *
     */
  async create() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(
      this,
      'carouselArr',
      'carouselGroupId'
    ).checkDataRes;

    try {
      if (checkRes) {
        const res = await service.web.carousel.create();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule(error);
    }
  }
  /**
     *
     * @api {DELETE} /api/carousel/delete 删除轮播图
     * @apiName 删除轮播图
     * @apiGroup 轮播图模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} id 轮播图id
     *
     *
     * @apiParamExample  {type} Request-Example:
        {
            "id":3
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "删除成功",
            "data": []
        }
     *
     *
     */
  async delete() {
    const { app, ctx, service } = this;
    try {
      const res = await service.web.carousel.delete();
      if (res == 'id不存在,删除失败') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }

  /**
     *
     * @api {GET} /api/carousel/get 获取单个轮播图
     * @apiName 获取单个轮播图
     * @apiGroup 轮播图模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} id 轮播图id
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 1,        //轮播图id
                "url": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",   //轮播图url
                "carouselGroupId": 1,   //轮播图分组id
                "projectId": 1,         //项目id
                "createdAt": "2021-11-11T08:29:20.000Z",
                "updatedAt": "2021-11-11T08:29:20.000Z"
            }
        }
     *
     *
     */
  async get() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'id').checkDataRes;
    if (checkRes) {
      const res = await service.web.carousel.get();
      return ctx.helper.successModule(res);
    }
  }

  async getAll() {
    const { ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, 'projectId').checkDataRes;

    if (checkRes) {
      const res = await service.web.carousel.getAll();
      return ctx.helper.successModule(res);
    }
  }

  async update() {
    const { ctx, service } = this;
    const res = await service.web.carousel.update();
    return ctx.helper.successModule(res);
  }
}
module.exports = CarouselController;
